package com.emm.yixun.mobile.ui.notice;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetNoticeDetail;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * <p>
 * <b>Title：</b> 公告详情界面
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 *
 * @author tangxin
 * @date 2016年12月6日 下午4:10:57
 */
public class NoticeTextContentActivity extends SwipeBackActivity {

    private static final String TAG = "Notice_Content";

    /**
     * title
     */
    TextView title_main;

    /**
     * back
     */
    ImageView back_btn;

    private String TITLE = "公告详情";

    /**
     * 通知标题
     */
    TextView title_content;

    /**
     * 时间
     */
    TextView time_date;

    /**
     * 内容
     */
    TextView content_text;

    private String noticeId;
    /**
     * 侧滑关闭框架
     */
    private SwipeBackLayout mSwipeBackLayout;

    @Override
    protected void onResume() {
        BaseActivity.activity = this;
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.noticetextcontent_activity);
        BaseActivity.activity = this;

        // 侧滑关闭框架配置方法
        SetSwipeBackLayout(mSwipeBackLayout);

        EmmApplication.IsReloadMain = true;
        TITLE = getIntent().getStringExtra("TITLE");
        noticeId = getIntent().getStringExtra("noticeId");
        Log.v(TAG, "noticeId-->" + noticeId);

        getNoticeDetail(noticeId);
        initView();

        SetRelativiLayoutPadding(R.id.main_head_top_rela);

        initBtn();
    }

    private void initView() {
        title_main = (TextView) findViewById(R.id.title_main);
        title_main.setText(TITLE);
        back_btn = (ImageView) findViewById(R.id.back_btn);
        title_content = (TextView) findViewById(R.id.title_content);
        time_date = (TextView) findViewById(R.id.time_date);
        content_text = (TextView) findViewById(R.id.content_text);
    }

    private void initBtn() {
        back_btn.setVisibility(View.VISIBLE);
        back_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    GetNoticeDetail getnoticedetail;

    private void getNoticeDetail(String noticeId) {
        EmmApplication.updateUrl("getNoticeDetail");
        Map<String, String> map = new HashMap<String, String>();
        map.put("version", EmmApplication.getPackageManager(5));
        map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
        map.put("token", EmmApplication.getToken());
        map.put("userId", EmmApplication.getUserId());
        map.put("terminalCode", EmmApplication.getPackageManager(2));
        map.put("projectCode", EmmApplication.getProjectCode());
        map.put("noticeId", noticeId);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("params", map);
        JSONObject json = new JSONObject(paramsMap);
        String jsonstr = json.toString();
        RequestParams params = new RequestParams();
        params.put("body", jsonstr);
        Log.v("getNoticeDetail-->post", EmmApplication.urlStr + "?body=" + jsonstr);
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束error:" + error + "==" + content);
            }

            @Override
            public void onFinish() {
                Loading.hideDialogForLoading();
                Log.v(TAG, "请求结束");
                super.onFinish();
            }

            @Override
            public void onStart() {
                super.onStart();
                Loading.hideDialogForLoading();
                Loading.showDialogForLoading(NoticeTextContentActivity.this, "正在加载...", false, true, 60000);
                Log.v(TAG, "请求开始");
            }

            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                if (content == null) {
                    return;
                }
                Log.v(TAG, "content:" + content);
                JSONObject jsonObj = JSONObject.parseObject(content);
                JSONObject resData = (JSONObject) jsonObj.get("resData");
                if (resData == null) {
                    Log.v(TAG, "信息返回值为空");
                    return;
                }

                getnoticedetail = JSONObject.parseObject(resData.toString(), GetNoticeDetail.class);
                if ("1".equals(getnoticedetail.getResult())) {
                    Log.v("获取成功", "");

                    title_content.setText(getnoticedetail.getNoticeTitle());

                    time_date.setText(getnoticedetail.getNoticeDate());

                    content_text.setText("    " + getnoticedetail.getNoticeContent());

                } else {
                    EmmApplication.T(getnoticedetail.getErrorMsg().toString());
                    try {
                        Log.v("获取失败", "getNoticeCategoryList-->errorCode:" + getnoticedetail.getErrorCode().toString() + "errorMsg:"
                                + getnoticedetail.getErrorMsg().toString());
                    } catch (NullPointerException e) {
                        Log.v("getNoticeCategoryList-->客户详情，错误信息获取空指针异常", "");
                    }
                }
            }
        });
    }

}
